﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS_Server
{
    class Action_SMS_Email:Incoming_Action
    {
        public string ActionName;
        public string ActionFormat;
        public string[] ParamNames = null;
        public string Address;
        public SendingMode Mode;

        public enum SendingMode
        {
           SMS, EMAIL
        }

        public bool GetResult(string PhoneNumber, object[] Params, out string result)
        {

            if (ActionFormat == "")
            {
                string str = "";
                foreach (string s in Params)
                {
                    if (str != "") str += " ";
                    str += s;
                }
                return GetResult(PhoneNumber, str, out result);
            }

            if (ParamNames == null)
            {
                ActionFormat = ActionFormat.Replace("[", "").Replace("]", "");
                ParamNames = ActionFormat.Split(' ');
            }
            if (Params.Length != ParamNames.Length)
            {
                string resp = "Please write in the given format " + ActionName.ToUpper();
                for (int i = 0; i < ParamNames.Length; i++)
                {
                    resp += " ";
                    resp += "[" + ParamNames[i] + "]";
                }
                result = resp; return true;
            }
            string comm = PhoneNumber.Replace("+", "") + " wrote " + ActionName.ToUpper();
            for (int i = 0; i < Params.Length; i++)
            {
                comm += " ";
                comm += Params[i];
            }

            if (Mode == SendingMode.EMAIL)
            {
                Global.SendEmail(Address, "noreply@smsserver.com", PhoneNumber, comm);
            }
            else if (Mode == SendingMode.SMS)
            {
                Global.SendSMS(Address, comm);
            }
            result = "";
            return false;
        }

        public bool GetResult(string PhoneNumber, string data, out string result)
        {
            string comm = PhoneNumber + " wrote " + data;

            if (Mode == SendingMode.EMAIL)
            {
                Global.SendEmail(Address, "noreply@smsserver.com", PhoneNumber, comm);
            }
            else if (Mode == SendingMode.SMS)
            {
                Global.SendSMS(Address, comm);
            }
            result = "";
            return false;
        }

    }
}
